package org.techmore.biz;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.techmore.utils.DownloadNoStoreHandler;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.CouponLoginVO;
import org.techmore.zxing.client.android.CaptureActivity;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class CouponLoginDataHandler {
	
	private static CouponLoginDataHandler couponLoginDataHandler = new CouponLoginDataHandler();
	
	public static CouponLoginDataHandler getInstance(){
		return couponLoginDataHandler;
	}
	
	private CouponLoginVO couponLoginVO = null;
	private Handler dataHandler = null;
	
	private SAXParserFactory factory = null;
	private SAXParser parser = null;
	
	public void login(final Context context, final Activity activity, final String userName, final String userPass){
		
		couponLoginVO = new CouponLoginVO();
		
		try {
			factory = SAXParserFactory.newInstance();
			parser = factory.newSAXParser();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			Log.e("CouponLoginDataHandler:", e.getMessage());
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			Log.e("CouponLoginDataHandler:", e.getMessage());
		}
		//&account=techmore&password=techmore
		String url = PropertiesUtil.getInstance().getValue("qr_login")+"&account="+userName+"&password="+userPass;
		
		dataHandler = new Handler(Looper.myLooper()){
    		public void handleMessage(android.os.Message msg) {
    			int sign = msg.what;
    			if(sign == 1){
    				String uri = String.valueOf(msg.obj);
        			try {
						parser.parse(new ByteArrayInputStream(uri.getBytes()), new CouponLoginVO_Handler());
					} catch (SAXException e) {
						Log.e("CouponLoginDataHandler:", e.getMessage());
					} catch (IOException e) {
						Log.e("CouponLoginDataHandler:", e.getMessage());
					}
					SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
					SharedPreferences.Editor editor = preferences.edit();
					if("true".equals(couponLoginVO.getUserInfo())){
						
						//本地副本变量 
						//CaptureActivityDataHandler使用
						CouponUserInfo.userLocal.set(userName);
						
						editor.putBoolean("isLogin", true);
						
						Intent intent = new Intent();
						intent.setClass(context, CaptureActivity.class);
						context.startActivity(intent);
						activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
						activity.finish();
					}
					else if("dismatch".equals(couponLoginVO.getUserInfo())){
						editor.putBoolean("isLogin", false);
						Toast.makeText(context, "登錄名或密碼不正確", Toast.LENGTH_LONG).show();
					}
					else{
						editor.putBoolean("isLogin", false);
						Toast.makeText(context, "登錄名或密碼不正確", Toast.LENGTH_LONG).show();
					}
					editor.commit();
    			}
    		}
		};
		dataHandler.postDelayed(new DownloadNoStoreHandler(url, dataHandler, 1), 0);
		
	}
	
	
	
	public class CouponLoginVO_Handler extends DefaultHandler{
		public CouponLoginVO_Handler(){}
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
		}
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			
			if("user".equals(qName) || "user".equals(localName) ){
				couponLoginVO.setUserInfo(attributes.getValue("userInfo"));
			}
		}
		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			super.endElement(uri, localName, qName);
			if("user".equals(qName) || "user".equals(localName)){
//				couponLoginVO = null;
			}
		}
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
		}
	}

}
